package com.ape9527.bot.controller;

import com.alibaba.fastjson.JSONObject;
import com.ape9527.bot.services.BotServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author YuanShuai[apeblog@163.com]
 */
@RestController
@Slf4j
public class CallbackController {

    private final BotServices botServices;

    public CallbackController(BotServices botServices) {
        this.botServices = botServices;
    }

    @PostMapping("")
    public void callback(@RequestBody String msg){
        JSONObject msgJson = JSONObject.parseObject(msg);
        switch (msgJson.getString("post_type")){
            case "message":
                // 消息
                parseMessage(msgJson);
                break;
            case "message_sent":
                // 消息发送
                break;
            case "request":
                // 请求
                break;
            case "notice":
                // 通知
                break;
            case "meta_event":
                // 元事件
                break;
            default:
                log.warn("未知类型消息：{}", msg);
        }
    }

    private void parseMessage(JSONObject msg) {
        String messageType = msg.getString("message_type");
        switch (messageType){
            case "private":
                // 私聊消息
                botServices.parsePrivateMessage(msg);
                break;
            case "group":
                // 群消息
                botServices.parseGroupMessage(msg);
                break;
            default:
                log.warn("未知消息类型：{}", messageType);
        }

    }

}
